// TypeScript file
module starlingswf
{
    /**
     * 动画更新管理器
     */
export class SwfUpdateManager
{
    private animations: ISwfAnimation[];
    private addQueue: ISwfAnimation[];
    private removeQueue: ISwfAnimation[];

    private fps: number;
    private fpsTime: number;
    private curTime: number;

    public static create(fps: number): SwfUpdateManager
    {
        let manager: SwfUpdateManager = new SwfUpdateManager();
        manager.animations = [];
        manager.addQueue = [];
        manager.removeQueue = [];
        manager.curTime = 0;
        manager.setFps(fps);

        egret.Ticker.getInstance().register(manager.update, manager);
        return manager;
    }

    public setFps(fps: number): void
    {
        this.fps = fps;
        this.fpsTime = 1000 / fps;
    }

    private clear(): void
    {
        this.addQueue.splice(0);
        this.removeQueue.splice(0);
        this.animations.slice(0);
    }

    public stop(): void
    {
        this.clear();
        egret.Ticker.getInstance().unregister(this.update, this);
    }

    public play(): void
    {
        egret.Ticker.getInstance().register(this.update, this);
    }

    public addSwfAnimation(animation: ISwfAnimation): void
    {
        this.addQueue.push(animation);
    }

    public removeSwfAnimation(animation: ISwfAnimation): void
    {
        this.removeQueue.push(animation);
        let idx: number = this.addQueue.indexOf(animation);
        if (-1 == idx)
        {
            return;
        }
        this.addQueue.splice(idx, 1);
    }

    private updateAdd(): void
    {
        let len: number = this.addQueue.length;
        let idx: number;
        let animation: ISwfAnimation;

        for (let i: number = 0; i < len; ++i)
        {
            animation = this.addQueue.pop();
            idx = this.animations.indexOf(animation);
            if (-1 == idx)
            {
                this.animations.push(animation);
            }
        }
    }

    private updateRemove():void
    {
        let len: number = this.removeQueue.length;
        let idx: number;
        let animation: ISwfAnimation;
        for (let i: number = 0; i < len; ++i)
        {
            animation = this.removeQueue.pop();
            idx = this.animations.indexOf(animation);
            if (-1 != idx)
            {
                this.animations.splice(idx, 1);
            }
        }
    }

    private update(time: number): void
    {
        this.curTime += time;
        if (this.curTime < this.fpsTime)
        {
            return;
        }

        this.curTime -= this.fpsTime;
        this.doUpdate();

        let jumpFlag: number = 0;
        while (this.curTime > this.fpsTime)
        {
            this.curTime -= this.fpsTime;
            ++jumpFlag;
            if (jumpFlag < 4)
            {
                this.doUpdate();
            }
        }
    }

    private doUpdate(): void
    {
        this.updateRemove();
        this.updateAdd();
        let len: number = this.animations.length;
        for (let i: number = 0; i < len; ++i)
        {
            this.animations[i].update();
        }
    }


}
}//end of module starlingswf